#coding:utf-8
from django.shortcuts import render_to_response

from lovewith.share.views.modules.attach import AttachApi
from lovewith.share.views.modules.share import ShareApi


#rss
def get_feeds(request):
    if request.method == 'GET':
        all_share_data = []
        post_data = ShareApi.get_yesterday()
        for p in post_data:
            share_cover = None
            post_attach = []
            attach_data = AttachApi.get_by_share(p.id)
            for img in attach_data:
                if img.is_cover:
                    share_cover = img.path
                post_attach.append(img.path)

            #封面
            if not share_cover and len(attach_data) > 0:
                share_cover = attach_data[0]

            all_share_data.append({
                'id': p.id,
                'title': p.title,
                'content': p.content,
                'author': p.user.username,
                'pub_date': p.post_date,
                'cover': share_cover,
                'images': post_attach,
                'is_video': p.is_video,
                'video': p.video
            })

        return render_to_response('share/rss.jinja', {
            'share_data': all_share_data
        }, mimetype="application/xml")